package com.jxnd.sunflower.task;

import com.jxnd.sunflower.mapper.RGoodsMapper;
import com.jxnd.sunflower.utils.RedisOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: 敷衍
 * @description:  定时任务
 * @date: 2021/12/7 11:03
 * @version: 1.0
 */
@Configuration  //1.标记配置类 使得springboot容器扫描到
@EnableScheduling
public class TaskGoods {

    @Autowired
    private RGoodsMapper goodsMapper;

    @Autowired
    private RedisOperator redis;

    // 添加定时任务 每天晚上11点执行一次任务 更新浏览量
    @Scheduled(cron = "0 0 23 * * ?")
    private void publishArticles() {

        //查询所有商品
        List list = goodsMapper.queryId();

        for (Object id:list){
            String count = redis.get(id.toString());
            //把商品浏览数更新进数据库
            goodsMapper.uodatelll( id.toString(),count);
        }


        System.out.println("执行定时任务：" + LocalDateTime.now());
    }
}
